package org.jili.novoland.game;

import org.jili.novoland.R;
import org.jili.novoland.base.LayerManager;
import org.jili.novoland.base.Menu;
import org.jili.novoland.base.MenuItem;
import org.jili.novoland.util.Cfg;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ResourceManager {

    static private ResourceManager instance = null;

    private Context context;


    private ResourceManager(Context context) {
        this.context = context;
    }

    static public ResourceManager getInstance(Context context) {
        if (instance == null) {
            instance = new ResourceManager(context);
        }

        return instance;
    }
    static public ResourceManager getInstance() {
        return instance;
    }
    public void loadTitle(LayerManager layerManager) {
        Menu title = initTitleMenu();
        layerManager.setMenu(title);

    }

    public void loadBattle(LayerManager layerManager) {
        Role leadingRole = initLeadingRole();
        layerManager.appendRole(leadingRole);

        BattleMap battleFieldMap = initMap();
        layerManager.setBattleFieldMap(battleFieldMap);

    }

    public Bitmap getPng(int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }

    public Context getContext() {
        return context;
    }


    private BattleMap initMap() {
        Bitmap mapPng = getPng(R.drawable.map);
        int mapMatrix[][] = { { 2, 2, 2, 2, 2, 1, 1, 3, 4, 1, 1, 1, 1, 2, 1, 2 },
                { 2, 1, 1, 1, 1, 1, 1, 3, 4, 4, 1, 1, 1, 2, 1, 2 }, { 2, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 1, 1, 2, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 }, { 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
                { 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 2 },
                { 4, 4, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 4, 1, 2 }, { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 }, { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 },
                { 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 2 } };
        int cols = mapMatrix[0].length;
        int rows = mapMatrix.length;

        BattleMap instance = new BattleMap(Cfg.getTileWidth(), Cfg.getTileHeight(), mapPng, cols, rows);
        for (int x = 0; x < cols; x++) {
            for (int y = 0; y < rows; y++) {
                instance.setCell(x, y, mapMatrix[y][x]);
            }
        }
        return instance;
    }

    private Role initLeadingRole() { 
        Bitmap rolePng = getPng(R.drawable.yuran);

        Role instance = new Role(Cfg.getTileWidth(), Cfg.getTileHeight(), rolePng);
        instance.setMatrixPosition(5, 8);
        instance.setAp(8);
        return instance;
    }

    private Menu initTitleMenu() {
        Bitmap menuPng = getPng(R.drawable.title);
        Bitmap itemPng = getPng(R.drawable.titleitems);
        MenuItem[] items = new MenuItem[3];;
        Menu instance = new Menu(menuPng);
        items[0] = new MenuItem(300, 300, Cfg.getTitleItemWidth(), Cfg.getTitleItemHeight(), itemPng, "开始");
        items[1] = new MenuItem(300, 360, Cfg.getTitleItemWidth(), Cfg.getTitleItemHeight(), itemPng, "制作团队");
        items[2] = new MenuItem(300, 420, Cfg.getTitleItemWidth(), Cfg.getTitleItemHeight(), itemPng, "推出");
        instance.setItems(items);
        instance.setVisible(true);
        return instance;
    }
}
